#!/usr/bin/make -f

DEB_SHLIBDEPS_INCLUDE := $(shell octave-config -p OCTLIBDIR) /usr/lib/atlas
DEB_COMPRESS_EXCLUDE  := .py .html .R .m
R_LIBRARY_DIR         := /usr/lib/R/site-library

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/patchsys-quilt.mk
include /usr/share/octave/debian/defs.make

PYVER     = $(shell pyversions -vr debian/control)
PYCONFIG  = cp -a src build-python$(modular)$(pyver); \
	     cd build-python$(modular)$(pyver); \
	     ./configure --disable-cpudetection --prefix=/usr \
		 --python=python$(pyver) --interface=python$(modular); \
	     cd ..;
PYBUILD   = $(MAKE) -C build-python$(modular)$(pyver);
PYTEST    = $(MAKE) -C build-python$(modular)$(pyver) tests \
			DESTDIR=$(CURDIR)/debian/shogun-python$(modular) || true
PYINSTALL = $(MAKE) -C build-python$(modular)$(pyver) install \
			DESTDIR=$(CURDIR)/debian/shogun-python$(modular);


makebuilddir:: debian/stamp-nonfree
debian/stamp-nonfree:
	@if [ -e src/classifier/svm/SVM_light.cpp ]; then \
	  echo "Do you really want to build a nonfree version? (y/N): "; \
	  read answer; \
	  if [ "$$answer" != y ]; then \
	    exit 1; \
	  fi; \
	fi
	touch $@

# doc
configure/shogun-doc:: debian/stamp-configure-doc
debian/stamp-configure-doc:
	mkdir build-doc
	cp -a doc src build-doc
	cd build-doc/src && ./configure --disable-cpudetection \
		--prefix=/usr --interface=python-modular
	touch $@

build/shogun-doc:: debian/stamp-build-doc
debian/stamp-build-doc:
	$(MAKE) -C build-doc/doc
	touch $@

install/shogun-doc:: debian/stamp-install-doc
debian/stamp-install-doc:
	mkdir -p $(CURDIR)/debian/shogun-doc/usr/share/doc/shogun-doc/html
	cd build-doc/doc/html && cp *.html *.png *.gif *.css $(CURDIR)/debian/shogun-doc/usr/share/doc/shogun-doc/html/
	touch $@

# python modular
configure/shogun-python-modular:: debian/stamp-configure-python-modular
debian/stamp-configure-python-modular: modular=-modular
debian/stamp-configure-python-modular:
	set -x; $(foreach pyver, $(PYVER), $(PYCONFIG))
	touch $@

build/shogun-python-modular:: debian/stamp-build-python-modular
debian/stamp-build-python-modular: modular=-modular
debian/stamp-build-python-modular:
	set -x; $(foreach pyver, $(PYVER), $(PYBUILD))
	touch $@

install/shogun-python-modular:: debian/stamp-install-python-modular
debian/stamp-install-python-modular: modular=-modular
debian/stamp-install-python-modular:
	set -x; $(foreach pyver, $(PYVER), $(PYINSTALL))
	set -x; $(foreach pyver, $(PYVER), $(PYTEST))
	dh_pycentral -pshogun-python-modular
	touch $@

# python
configure/shogun-python:: debian/stamp-configure-python
debian/stamp-configure-python: modular=
debian/stamp-configure-python:
	set -x; $(foreach pyver, $(PYVER), $(PYCONFIG))
	touch $@

build/shogun-python:: debian/stamp-build-python
debian/stamp-build-python: modular=
debian/stamp-build-python:
	set -x; $(foreach pyver, $(PYVER), $(PYBUILD))
	dh_pycentral -pshogun-python
	touch $@

install/shogun-python:: debian/stamp-install-python
debian/stamp-install-python: modular=
debian/stamp-install-python:
	set -x; $(foreach pyver, $(PYVER), $(PYINSTALL))
	touch $@

# readline
configure/shogun-readline:: debian/stamp-configure-readline
debian/stamp-configure-readline:
	cp -a src build-readline
	cd build-readline && ./configure --disable-cpudetection \
		--prefix=/usr --interface=cmdline
	touch $@

build/shogun-readline:: debian/stamp-build-readline
debian/stamp-build-readline:
	$(MAKE) -C build-readline
	touch $@

install/shogun-readline:: debian/stamp-install-readline
debian/stamp-install-readline:
	$(MAKE) -C build-readline install DESTDIR=$(CURDIR)/debian/shogun-readline
	touch $@

# octave
configure/shogun-octave:: debian/stamp-configure-octave
debian/stamp-configure-octave:
	# reintroduce tests; failing autobuilders are fun ...
	ls -l /usr/bin/oct*
	ls -l /etc/alternatives/oct*
	cp -a src build-octave
	cd build-octave && ./configure --disable-cpudetection \
		--prefix=/usr --interface=octave
	touch $@

build/shogun-octave:: debian/stamp-build-octave
debian/stamp-build-octave:
	$(MAKE) -C build-octave
	touch $@

install/shogun-octave:: debian/stamp-install-octave
debian/stamp-install-octave:
	dh_install -pshogun-octave build-octave/sg.oct $(OCTDIR)
	octave-depends
	touch $@

# octave-modular
configure/shogun-octave-modular:: debian/stamp-configure-octave-modular
debian/stamp-configure-octave-modular:
	# reintroduce tests; failing autobuilders are fun ...
	ls -l /usr/bin/oct*
	ls -l /etc/alternatives/oct*
	cp -a src build-octave-modular
	cd build-octave-modular && ./configure --disable-cpudetection \
		--prefix=/usr --interface=octave-modular
	touch $@

build/shogun-octave-modular:: debian/stamp-build-octave-modular
debian/stamp-build-octave-modular:
	$(MAKE) -C build-octave-modular
	touch $@

install/shogun-octave-modular:: debian/stamp-install-octave-modular
debian/stamp-install-octave-modular:
	$(MAKE) -C build-octave-modular install \
			DESTDIR=$(CURDIR)/debian/shogun-octave-modular;
	octave-depends
	touch $@

# r
configure/shogun-r:: debian/stamp-configure-r
debian/stamp-configure-r:
	mkdir build-r
	cp -a R src build-r
	$(RM) -r build-r/R/sg/src
	touch $@

build/shogun-r:: debian/stamp-build-r
debian/stamp-build-r:
	$(MAKE) -C build-r/R sg/src CFGOPTS=--disable-cpudetection
	find build-r/R/sg \( -name COPYING -o -name 'LICENSE*' \) -delete
	touch $@

install/shogun-r:: debian/stamp-install-r
debian/stamp-install-r:
	mkdir -p $(CURDIR)/debian/shogun-r$(R_LIBRARY_DIR)
	R CMD INSTALL -l $(CURDIR)/debian/shogun-r$(R_LIBRARY_DIR) build-r/R/sg
	touch $@

get-orig-source:
	-uscan --rename --upstream-version 0
	@echo successfully retrieved upstream tarball

clean::
	$(RM) -r build-* debian/stamp-*
	find debian -mindepth 2 -name 'shogun-*' -delete
